package com.team.controller;

import java.util.Calendar;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.team.dao.impl.EquipmentStockDaoImpl;
import com.team.entity.CapitalReport;
import com.team.entity.Equipment;
import com.team.entity.EquipmentStock;
import com.team.service.AdminiStrationService;
import com.team.service.Impl.EquipmentServiceImpl;
import com.team.service.Impl.EquipmentStockServiceImpl;
import com.team.util.EquipmentPage;

@Controller
public class EquipmentController {
	@Resource
	private EquipmentServiceImpl el;
	@Resource
	private EquipmentStockServiceImpl esl;
	@Resource
	private AdminiStrationService adminiStrationServiceImpl;

	@RequestMapping(value = "addequipent", method = RequestMethod.POST)
	public String addequipent(Equipment equipment) {
		EquipmentStock equipmentStock = esl.selectEquipmentStockByName(equipment.getName());
		if (equipmentStock == null) {
			return "/equipmentfail.jsp";
		} else {
			el.addEquipment(equipment);
			return "/listequipment";
		}

	}

	@RequestMapping(value = "selectequipment", method = RequestMethod.GET)
	public ModelAndView selectequipment(ModelAndView view, int id) {
		Equipment equipment = el.selectEquipmentById(id);
		view.addObject("equipment", equipment);
		view.setViewName("/showequipment.jsp");
		return view;

	}

	@RequestMapping(value = "listequipment")
	public String listequipment(Model model,HttpSession session,HttpServletRequest request) {
		EquipmentPage equipmentPage=(EquipmentPage)session.getAttribute("equipmentPage");
		String a="shouye";
        String b="shangyiye";
		String c="xiayiye";
		String statu = request.getParameter("status");
		if(equipmentPage==null||statu==null){
			equipmentPage=new EquipmentPage();
			equipmentPage.setPagenumber(0);
		}else{
			if(statu.equals(a)){
				equipmentPage.setPagenumber(0);
			}else if (statu.equals(b)) {
				if(equipmentPage.getPagenumber()!=0){
					equipmentPage.setPagenumber(equipmentPage.getPagenumber()-1);
				}else {
					equipmentPage.setPagenumber(0);
					}
				}else if (statu.equals(c)) {
					equipmentPage.setPagenumber(equipmentPage.getPagenumber()+1);
					if(el.selectAllEquipment(equipmentPage.getPagenumber()).isEmpty()){
						List<Equipment> list = el.selectAllEquipment(equipmentPage.getPagenumber()-1);
						equipmentPage.setPagenumber(equipmentPage.getPagenumber()-1);
						model.addAttribute("list", list);
						return "/listequipment.jsp";
					}
					
				}
			}
		List<Equipment> list = el.selectAllEquipment(equipmentPage.getPagenumber());
		model.addAttribute("list", list);
		session.setAttribute("equipmentPage", equipmentPage);
		return "/listequipment.jsp";
		

	}

	@RequestMapping(value = "delequipment/{id}")
	public String delequipment(Model model, @PathVariable String id) {
		el.deleteEquipment(Integer.parseInt(id));
		return "redirect:/listequipment";

	}

	@RequestMapping("seleteupdate")
	public String seleteupdate(Model model, String id) {
		Equipment sequipment = el.selectEquipmentById(Integer.parseInt(id));
		model.addAttribute("sequipment", sequipment);
		return "/updateequipment.jsp";
	}

	@RequestMapping("seleterepaire") 
	public String seleterepaire(Model model, String id) {
		Equipment srequipment = el.selectEquipmentById(Integer.parseInt(id));
		model.addAttribute("srequipment", srequipment);
		return "/repairequipment.jsp";
	}

	@RequestMapping("updateequipment")
	public String updateequipment(Equipment equipment) {
		el.updateEquipment(equipment);
		return "/listequipment";
	}

	// 申请新设备
	@RequestMapping("addequipentstock")
	public String addequipentstock(EquipmentStock equipmentStock) {
		CapitalReport capitalReport = new CapitalReport();
		Calendar calendar = Calendar.getInstance();
		int year = calendar.get(Calendar.YEAR);
		int month = calendar.get(Calendar.MONTH);
		int day = calendar.get(Calendar.DATE);
		if (month == 1 || month == 2 || month == 3) {
			capitalReport.setSeason("春季");
		} else if (month == 4 || month == 5 || month == 6) {
			capitalReport.setSeason("夏季");
		} else if (month == 7 || month == 8 || month == 9) {
			capitalReport.setSeason("秋季");
		} else if (month == 10 || month == 11 || month == 12) {
			capitalReport.setSeason("冬季");
		}
		capitalReport.setDay(day);
		capitalReport.setYear(year);
		capitalReport.setMonth(month + 1);
		capitalReport.setMoney(equipmentStock.getPrice() * equipmentStock.getStock());
		capitalReport.setDescription(equipmentStock.getName());
		capitalReport.setStock(equipmentStock.getStock());
		adminiStrationServiceImpl.saveCapitalReport(capitalReport);
		if (esl.addEquipmentStock(equipmentStock)) {

			return "/equipmentsuccess.jsp";
		} else {
			return "/equipmentfail.jsp";
		}
	}

	@RequestMapping("selectasset")
	public String selectasset(Model model) {
		Integer asset = esl.seleteAsset();
		model.addAttribute("asset", asset);
		return "selectassetjsp.jsp";
	}

}
